<template>
    <el-card class="w-full pt-[5px]">
        <div class="mb-[20px] flex justify-end">
            <el-button type="primary" @click="handleAdd">
                <el-icon class="mr-[5px]">
                    <Plus />
                </el-icon>
                新增
            </el-button>
            <el-button @click="handleDel">
                <el-icon>
                    <DeleteFilled />
                </el-icon>
                删除
            </el-button>
        </div>
        <el-table :data="tableData" class="w-full" @selection-change="handleSelectionChange" stripe>
            <el-table-column type="selection" width="55" :selectable="checkSelectable" />
            <el-table-column prop="name" label="评审方案名称">
                <template #default="scope">
                    <el-link type="primary" @click="viewItem(scope.row)">{{ scope.row.name }}</el-link>
                </template>
            </el-table-column>
            <el-table-column prop="isUsed" label="使用状态" />
            <el-table-column fixed="right" label="操作" width="300" align="center">
                <template #default="scope">
                    <el-button type="primary" size="small" plain @click="handleEdit(scope.row)">
                        编辑
                    </el-button>
                    <el-button type="primary" size="small" plain @click="delHonor(scope.row)"
                        v-if="scope.row.isUsed != '是'">删除</el-button>
                </template>
            </el-table-column>
        </el-table>
        <div class="mt-[20px] flex justify-end">
            <pagination v-show="total > 0" :total="total" v-model:page="pageInfo.pageNum"
                v-model:limit="pageInfo.pageSize" @pagination="getList" />
        </div>
    </el-card>
    <psfaEdit v-model="editModelVisible" ref="EditModalRef" @callBack="callBackFun" />
    <psfaview v-model="ViewdialogVisible" ref="viewModalRef" />

</template>

<script setup>
import { onMounted } from 'vue';
import { useRouter } from "vue-router";
import { ElMessage, ElMessageBox } from "element-plus";
import { getreviewSchemeList, reviewSchemeDel, getreviewSchemeInfo, getreviewStandardInfo, getreviewStandardList } from "@/api/achievement/cghj"
import psfaEdit from "./psfaEdit.vue";
import psfaview from "./psfaview.vue";
const router = useRouter();
const editModelVisible = ref(false);
const ViewdialogVisible = ref(false);
const dialogVisible = ref(false);
const searchForm = ref({
});
const pageInfo = ref({
    pageNum: 1,
    pageSize: 10
})
const total = ref(0)
const tableData = ref([]);
//点击新增
const handleAdd = () => {
    editModelVisible.value = true;
};
const EditModalRef = ref(null)
//点击编辑
const handleEdit = (i) => {

    getreviewSchemeInfo(i.id).then(res => {
        if (res.code == 200) {
            let zcData = res.data

            getreviewStandardList({ reviewSchemeId: i.id }).then(res1 => {
                if (res1.code == 200) {
                    editModelVisible.value = true;
                    zcData.reviewStandardList = res1.rows
                    EditModalRef.value.getRuleForm(zcData)
                }
            })

        }
    })
};


const viewModalRef = ref(null)
const viewItem = (i) => {
    getreviewSchemeInfo(i.id).then(res => {
        if (res.code == 200) {
            let zcData = res.data
            getreviewStandardList({ reviewSchemeId: i.id }).then(res1 => {
                if (res1.code == 200) {
                    zcData.reviewStandardList = res1.rows
                    ViewdialogVisible.value = true;
                    viewModalRef.value.getRuleForm(zcData)
                }
            })

        }
    })
};
const multipleSelection = ref([])
const handleSelectionChange = (val) => {
    multipleSelection.value = val
}
//点击删除
const handleDel = () => {
    if (multipleSelection.value.length == 0) {
        ElMessage.warning("请选择要删除的数据")
        return;

    } else {
        ElMessageBox.confirm("是否确认删除？", "提示", {
            type: "warning",
        }).then(() => {
            let ids = multipleSelection.value.map(item => item.id)
            ids = ids.join(',')
            reviewSchemeDel(ids).then(res => {
                if (res.code == 200) {
                    getList()
                    ElMessage.success('删除成功')
                }
            })
        });
    }

};
const initList = () => {
    getList()
}
const getList = async (i) => {
    if (i == 1) {
        pageInfo.value.pageNum = 1
    }
    let parms = {
        ...searchForm.value,
        ...pageInfo.value
    }
    let res = await getreviewSchemeList(parms)
    if (res.code == 200) {
        tableData.value = res.rows
        total.value = res.total
        console.log(tableData.value)
    }
}
const callBackFun = () => {
    getList()
}
const delHonor = (i) => {
    ElMessageBox.confirm(
        '确认！删除这条数据?',
        '提醒',
        {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning',
        }
    )
        .then(() => {
            reviewSchemeDel(i.id).then(res => {
                if (res.code == 200) {
                    getList()
                    ElMessage.success('删除成功')
                }
            })


        })
        .catch(() => {

        })

}
const checkSelectable = (row, index) => {
    return row.isUsed != '是';
}


onMounted(() => {
    initList()
});
</script>
